﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SKYPE4COMLib;
using HistoryToMail;

namespace SkypeHistoryToMailBU
{
    public partial class MainFrom : Form
    {
        HistoryToMailEngine mengine = new HistoryToMailEngine();
        Boolean reallyClose = false;

        private static Skype skype = new Skype();
        public MainFrom()
        {
            InitializeComponent();
        }

        private void MainFrom_Load(object sender, EventArgs e)
        {
            AttachSkypeEvents();

            mengine.SettingsChanged += new PluginSettingWhereChanged(SettingsWhereChanged);

            try
            {
                mengine.loadSettingsFromString(Properties.Settings.Default.HistoryEngineSettings);
            }
            catch (Exception /*exception*/)
            {
            }

            try
            {
                skype.Attach(8, false);
            }
            catch (Exception exception)
            {
                // Write Application Connecting Event Handler Failed to Window.
                textBox1.Text = DateTime.Now.ToLocalTime() + ": " +
                 "Application Connecting Event Handler Failed" +
                 " - Exception Source: " + exception.Source + " - Exception Message: " + exception.Message +
                 "\r\n";

            }

            
        }

        private void AttachSkypeEvents()
        {
            try
            {
                // Try setting our custom event handler while avoiding any ambiguity.
                ((_ISkypeEvents_Event)skype).ApplicationConnecting += OurApplicationConnecting;
            }
            catch (Exception exception)
            {
                // Write Application Connecting Event Handler Failed to Window.
                textBox1.Text = DateTime.Now.ToLocalTime() + ": " +
                 "Application Connecting Event Handler Failed" +
                 " - Exception Source: " + exception.Source + " - Exception Message: " + exception.Message +
                 "\r\n";

            }

            try
            {
                // Try setting our custom event handler while avoiding any ambiguity.
                ((_ISkypeEvents_Event)skype).MessageStatus += MessageStatus;
            }
            catch (Exception exception)
            {
                // Write Application Connecting Event Handler Failed to Window.
                textBox1.Text = DateTime.Now.ToLocalTime() + ": " +
                 "Application Connecting Event Handler Failed" +
                 " - Exception Source: " + exception.Source + " - Exception Message: " + exception.Message +
                 "\r\n";

            }
        }


        public void OurApplicationConnecting(SKYPE4COMLib.Application application, UserCollection usercollection)
        {
            // Always use try/catch with ANY Skype calls.
            try
            {
                // Write Application Connecting to Window.
                textBox1.Text = DateTime.Now.ToLocalTime() + ": " +
                 "Application Connecting: " + application.Name +
                 " - User Collection Count: " + usercollection.Count +
                 "\r\n";
            }
            catch (Exception e)
            {
                // Possibly old Skype4COM version, log an error, drop into debug if wanted.
                textBox1.Text = DateTime.Now.ToLocalTime() + ": " +
                 "Application Connecting Event Fired - Bad Text " +
                 " - Exception Source: " + e.Source + " - Exception Message: " + e.Message +
                 "\r\n";
            }
        }


     public void MessageStatus(ChatMessage pMessage, TChatMessageStatus Status)
    {
        textBox1.Text += "\r\n" + pMessage.FromDisplayName + ":" + pMessage.Body + "\r\n";

         if(Status == TChatMessageStatus.cmsReceived){
             mengine.addMessage("Skype", pMessage.Chat.FriendlyName, pMessage.Chat.DialogPartner, MessageType.inp, pMessage.Body, pMessage.Timestamp);
         }
         else if (Status == TChatMessageStatus.cmsSent)
         {
             mengine.addMessage("Skype", pMessage.Chat.FriendlyName, "me", MessageType.outp, pMessage.Body, pMessage.Timestamp);
         }
    }

     private void MainFrom_FormClosing(object sender, FormClosingEventArgs e)
     {
         if (reallyClose)
         {
             mengine.Stop();
         }
         else
         {
             e.Cancel = true;
             MinimizeToTray();
         }
     }

     private void propertiesButton_Click(object sender, EventArgs e)
     {
         mengine.showSettingsDialog();
     }

     private void SettingsWhereChanged(String settings)
     {
         try
         {
             Properties.Settings.Default.HistoryEngineSettings = settings;
             Properties.Settings.Default.Save();
         }
         catch (Exception /*exception*/)
         {
         }
     }

     private void notifyIcon_DoubleClick(object sender, EventArgs e)
     {
         Restore();
     }

     private void Restore()
     {
         
         Show();
         WindowState = FormWindowState.Normal;
     }

     private void hideButton_Click(object sender, EventArgs e)
     {
         MinimizeToTray();
     }

     private void MinimizeToTray()
     {
         Hide();
     }

     private void exitButton_Click(object sender, EventArgs e)
     {
         reallyClose = true;
         Close();
     }

     private void MainFrom_Resize(object sender, EventArgs e)
     {
         if (WindowState == FormWindowState.Minimized)
         {
             MinimizeToTray();
         }
         else
         {
             Restore();
         }
     }

    }
}
